package com.dzwww.recommend.rule.dao.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 *  自定义填充公共字段
 * <p>
 *   @author lixin
 *   @date 2018/3/28 下午7:46
 *   @version 1.0
 */
@Component
public class MetaObjectHandlerConfig implements MetaObjectHandler {

    /**
     * 插入填充
     *
     * @param metaObject metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        Object feature = getFieldValByName("feature", metaObject);
        if (feature == null) {
            setFieldValByName("feature", "", metaObject);
        }

        Object status = getFieldValByName("status", metaObject);
        if (status == null) {
            setFieldValByName("status", 0, metaObject);
        }

        Object deleted = getFieldValByName("deleted", metaObject);
        if (deleted == null) {
            setFieldValByName("deleted", 0, metaObject);
        }

        Object gmtCreate = getFieldValByName("gmtCreate", metaObject);
        if (gmtCreate == null) {
            setFieldValByName("gmtCreate", new Date(), metaObject);
        }

        Object gmtModified = getFieldValByName("gmtModified", metaObject);
        if (gmtModified == null) {
            setFieldValByName("gmtModified", new Date(), metaObject);
        }
    }

    /**
     * 更新填充
     * <p>
     * 已在公共字段设置，这里不需要再设置
     *
     * @param metaObject metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
    }

}
